# like the filename says, this chunks some fuckin images
##################################################
# does importing modules everywhere use extra memory?
import cv # opencv bindings; installed in package manager
import sys # system specific crap, LIKE sys.argv etc.
import os # system interfaces
import string
import numpy
##################################################

from HelperFunctions import *
from WormCountConfigs import *

##################################################

#def ChunkImages(configs = Configs()):

  # fake declarations just by way of documentation
  #label = '' # photo filename without extension
  #datafile = None # this will be the obkects file from imagej
  #objects = None # numpy (lol) array of this file
  #cropped = None # this is the cropped region
  #thisimage = None # plate photo; CvMat type

  # assuming errything's an image... we'll actually try/catch in the import
  #print os.listdir(configs.here+configs.picturepath)

  #files = [ f for f in os.listdir(configs.here+configs.picturepath) if os.path.isfile(f) ] # not dirs
  # ^^^ FAILS unless you're actually in the same directory. NO IDEA why. But ^^^ works for not(isfile(...))
  #os.chdir(configs.here+configs.picturepath)
  #files = [ f for f in os.listdir(configs.here) if os.path.isfile(f) ]
  #os.chdir('..')

#  print 'loading plate pics: '+str(files)

  #for platephoto in files: #for all plate photos
    #thisimage = cv.LoadImageM(configs.here+configs.picturepath+platephoto)
    #label = platephoto.split('.')[0] # filename of photo without the extension

    ## will want to make this more flexible..
    ##datafile = open(childprefix+'/'+configs.dataprefix+string.replace(platephoto,'tif','tsv'))
    #datafile = open(configs.outputpath+configs.dataprefix+label+configs.datasuffix)

    #objects = numpy.genfromtxt(datafile,skiprows=1)
    ##datafile.close() # don't need this after it's copied
    #MkDir(configs.here+configs.outputpath+configs.objectprefix+label)

    ## ima have to refactor this into anobject class

    #for index in range(len(objects[:,1])):
      #thisobject = objects[index,:]
      #bounding_rect = tuple(thisobject[6:10])
      ## maybe the rectangle should be extended by a 1px boundary,
      ## to avoid edge efx. but that's a pain.
      #cropped = cv.GetSubRect(thisimage, bounding_rect)
      #cv.SaveImage(configs.here+configs.outputpath+configs.objectprefix+label+'/'+str(index)+configs.savetype, cropped)

